/**
 * Created by kroyo on 2017/8/18.
 * 用户中心页
 */



//上传公司标志
$('.btn-upload').click(function(){
    var $file = $('.upload-logo input');
    var $img = $(".upload-logo img");
    $file.on('click',function(event) {
        $(this).change(function(){
            changefile(this);
        })
    });
    $file.trigger('click');
})
//装换file地址的方法
function changefile($this){
    var objUrl = getObjectURL($this.files[0]) ;
    if (objUrl) {
        $(".upload-logo img").attr("src", objUrl) ;
    }
    function getObjectURL(file) {
        var url = null ;
        if (window.createObjectURL!=undefined) { // basic
        url = window.createObjectURL(file) ;
        } else if (window.URL!=undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file) ;
        } else if (window.webkitURL!=undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file) ;
        }
        return url ;
    }    
}

//1.资料页面
//设置密码
$('#container-other .set-password').click(function(){
    overfhidden();
    $('#web .win-modpassword').fadeIn();
})


//清除数据
closeWindow('.win-modpassword');
closeWindow('.win-setpassword');
function closeWindow(sel){
    $('#web '+sel+' .btn-reset').click(function(){
        $('#web '+sel).fadeOut();
        $('#web '+sel+' input[type=reset]').trigger('click');
        overfauto();
    })
}


//添加窗口
addBtn('.link-man','.win-addcontact');
addBtn('.company-cert','.win-uploadfile');
function addBtn(sel,sel2){
    $('#container-other '+sel+' .addbtn').click(function () {
        if(sel2=='.win-addcontact'){
            $('#web '+sel2+' h4').text('添加联系人');
        }else if(sel2=='.win-uploadfile'){
            $('#web '+sel2+' h4').text('上传文件');
        }else{
            $('#web '+sel2+' h4').text('添加品类');
        }
        $('#web '+sel2).fadeIn();
        overfhidden();
    })
}
//关闭窗口
closeBtn('.win-addcontact');
closeBtn('.win-uploadfile');
function closeBtn(sel){
    $('#web '+sel+' .btn-close').click(function(){
        $('#web '+sel).fadeOut();
        overfauto();    
        $('#web '+sel+' input[type=reset]').trigger('click');
    })
}



//编辑窗口
modBtn('.link-man','.win-addcontact');
modBtn('.company-activity','.win-category');
function modBtn(sel,sel2){
    $('#container-other '+sel+' .btn-mod').click(function () {
        if(sel2=='.win-addcontact'){
            $('#web '+sel2+' h4').text('编辑联系人');
        }else{
            $('#web '+sel2+' h4').text('编辑品类');
        }
        $('#web '+sel2).fadeIn();
        overfhidden();
    })
}



//删除按钮的提示
btndel('.company-activity');
btndel('.company-cert');
function btndel(sel){
    $('#container-other '+sel+' .btn-del').click(function(e){
        var $title = $('#web .win-delalert .title');
        if(sel=='.company-activity'){
            $title.text('确定删除营业品类?');
        }else{
            $title.text('确定删除认证证件?');
        }
        $('#web .win-delalert').fadeIn();
        overfhidden();
    })
}

//删除按钮的取消
$('#container-other .btn-re').click(function () {
    $('#web .win-delalert').fadeOut();
    overfauto();
})


//弹出弹框时，隐藏于显示滚动条
function overfhidden(){
    $('body').css('overflow','hidden');
}
function overfauto(){
    $('body').css('overflow','auto');
}


//微信二维码弹出
var $win_weichat = $('#web .win-weichat');
$('.link-man .weixin-code').click(function(){
    $win_weichat.fadeIn();
    overfhidden();

    weichat();
});
function weichat(){

    $('#web .win-weichat .weichat-wrap').click(function(event) {
        event.stopPropagation();
    });

    $win_weichat.click(function(){
        $win_weichat.fadeOut();
        overfauto();
    });
}




//省市区三级联动 获取数据
//var data = {};
//$.ajax({
//  type:'get',
//  url:'../../data/city-data.json',
//  async:false,
//  success:function(res){    //成功得到返回时运行
//      data = res;
//      return data;
//  },
//  ajaxError:function(a,b,c){
//      console.log(a);      //错误的对象
//      console.log(b);      //错误的类型
//      console.log(c);      //错误的信息
//  }
//})
////循环数据
//dataCity('.ftt',data);
//clickactive('.ftt');
//function dataCity(sel,data){
//  $('.addresslist '+sel+' li').remove();
//  for(var i = 0;i<data.length;i++){
//      $('.addresslist '+sel).append('<li>'+data[i].value+'</li>');
//  }
//}


var $address = $('#container-other .select-city');
var $select = $('#container-other .select');

$('#web').on('click',function(e) {
    var target = e.target;
    switch (true){
        //1.人数选择框
        case hasClass(target.parentNode.parentNode,'company-pnum'):
            selectdown('.company-pnum .text','.list-down','.company-pnum .select');
            $address.removeClass('active');
            $('#container-other .addresslist').slideUp();
            break;
        //2.地址选择
        case hasClass(target.parentNode,'select-city'):
            selectdown('.address','.addresslist','.select-city');
            $select.removeClass('active');
            $('#container-other .list-down').slideUp();

            //回到原来选择的地方
            addactive();
            break;
        //省市级联
        case hasClass(target.parentNode,'ftt'):
            showcity('.first');
            $('.addresslist .third').css('display','none');
            // selectdown('.address','.addresslist','.select-city','.first');
            break;
        case hasClass(target.parentNode,'stt'):
            // ondown = true;
            // selectdown('.address','.addresslist','.select-city','.second');
            showcity('.second');
            break;
        case hasClass(target.parentNode,'tht'):
            ondown = true;
            hiddenSelect();
            break;
        default:
            ondown = true;
            $address.removeClass('active');
            $select.removeClass('active');
            $('#container-other .list-down').slideUp();
            $('#container-other .addresslist li').removeClass('active');
            $('.addresslist .second').css('display','none');
            $('.addresslist .third').css('display','none');
            $('#container-other .addresslist').slideUp();
            //省市级联未保存则回到原来状态
            addactive();
    }
})
//2.事件委托 所需的判断 判断是否存在某个class
function hasClass(target,selector){
    var val = target.className.indexOf(selector);
    if(val>=0){
        return true;
    }else{
        return false;
    }
}

var ondown = true;
function selectdown(selis,selector,sel){
    var $sel = $('#container-other '+sel);
    var $select = $('#container-other '+selector);
    var $selist = $('#container-other '+selector+' li');
    var $sel_wrap = $('#container-other '+selis);

    //判断选项卡时弹出还是隐藏  单击弹出(off=true)  双击隐藏(off=false)
    if(ondown){
        $select.slideDown();
        $sel.addClass('active');
        ondown = false;
    }else{
        $select.slideUp();
        $sel.removeClass('active');
        $selist.removeClass('active');
        $('.addresslist .second').css('display','none');
        $('.addresslist .third').css('display','none');
        ondown = true;
    }

    //弹出的窗体(即选项卡)  当选择了时，标记选择并进行显示所选项的数据更新()
    if(sel=='.company-pnum .select'){
        $selist.on('click',function(){
            var id = $(this).index();
            $selist.removeClass('active');
            $selist.eq(id).addClass('active');
            $sel_wrap.val($(this).text());

            //关闭未填写提示文字
            alerthint(selArr[1],1);
        })
    }else{
        $selist.on('click',function(){
            var parent = $(this).context.parentNode;
            var city ;
            if(hasClass(parent,'first')){
                city = '.addresslist';
                $('.addresslist .third').css('display','none');
            }
            if(hasClass(parent,'second')){
                city = '.second';
            }
            if(hasClass(parent,'third')){
                city = '.third';
            }
            $('#container-other '+city+' li').removeClass('active');
            $(this).addClass('active');
        })
    }
}

//省市级联 添加active
//省市级联未保存则回到原来状态
function addactive(){
    var $list = $('#container-other .addresslist li');
    //首先清空所有active
    if(index.length>0){
        $list.removeClass('active');

        var a = index[0],b= index[1]-31,c = index[2];
        dataCity('.stt',data[a].children);
        dataCity('.tht',data[b].children[c].children);

        $('#container-other .first li').eq(a).addClass('active');
        $('#container-other .second li').eq(b).addClass('active');
        $('#container-other .third li').eq(c).addClass('active');

        //展开所有选项卡
        $('.addresslist .second').css('display','block');
        $('.addresslist .third').css('display','block');

        return true;
    }
}

//判断出现市区
function showcity(sel){
    var value;
    if(sel=='.first'){
        value = '.second';
        dataCity('.stt',data[pactive('.ftt')].children);
    }
    if(sel=='.second'){
        value = '.third';
        dataCity('.tht',data[pactive('.ftt')].children[pactive('.stt')].children);
    }
    //给区县添加点击事件
    clickactive(value);
    $('.addresslist '+value).css('display','block');
    scroll(value);
}

//当选择了区了，收起并保存地址
var index = [];
function hiddenSelect() {
    var $list = $('#container-other .addresslist li'); ;
    var arr = [];
    index.length = 0;

    for(var i = 0;i<$list.length;i++){
        if($list.eq(i).hasClass('active')){
            arr.push($list.eq(i).text());
            index.push(i);
            if(index.length==2){
                break;
            }
        }
    }

    var $lis = $('#container-other .third li');
    for(var i = 0;i<$lis.length;i++){
        if($lis.eq(i).hasClass('active')){
            arr.push($lis.eq(i).text());
            index.push(i);
        }
    }

    $address.removeClass('active');
    $('#container-other .addresslist').slideUp();
    $('.select-city .address').val(arr[0]+'/'+arr[1]+'/'+arr[2]);

    //关闭未填写提示文字
    alerthint(selArr[4],4);

    return index;
}
//循环判断选中了那个
var num;
function pactive(sel){
    var $city = $('.addresslist '+sel+' li');
    for(var i =0;i<$city.length;i++){
        if($city.eq(i).hasClass('active')){
            num = i;
            return num;
        }
    }
}
//省市区 选中添加active
function clickactive(sel){
    var $list = $('.addresslist '+sel+' li');
    $list.click(function () {
        $list.removeClass('active');
        $(this).addClass('active');
    })
}



//资料 必填项问题
var $save_btn = $('.content-info .btn-save');
var selArr = ['.company-name','.company-pnum','.company-time','.company-phone','.company-site','.company-activity'];
var $infohint = $('.content-info .hint');
$save_btn.click(function(){
    //必填项都填了 则可以保存
    mustInfo();
})

function mustInfo(){
    var valArr = [];

    for(var i = 0;i<selArr.length;i++){
        valArr.push(alerthint(selArr[i],i));
    }
    
    if(valArr[0]&&valArr[1]&&valArr[2]&&valArr[3]&&valArr[4]&&valArr[5]){
        return true;
    }else{
        return false;
    }
}
function alerthint(sel,num){
    var $value = $(sel+' input').first();
    var val ;

    if(num===selArr.length-1){
        $value = $(sel+' .datalist');
        val = $value.length;
    }else{
        val = $value.val();
    }
    if(val==''||val==0){
        $infohint.eq(num).css('display','block');
        $value.css('border-color','red');

        $('.alert-view').css('top','-100px');
        $('.alert-view').css('opacity','0');

        return false;
    }else{
        $infohint.eq(num).css('display','none');
        $value.css('border-color','#cccccc');
        return true;
    } 
}
//5.1 监听必填是否输入
for(var i = 0 ;i<selArr.length-1;i++){
    listener(selArr[i],i);
}
function listener(sel,num){
    $(sel+' input').bind('input propertychange', function() {
        //进行相关操作
        alerthint(sel,num);
    });
}



changBox('.user-center input[type=checkbox]');
changBox('.win-category input[type=checkbox]');
function changBox(sel){
    var $input = $(sel);
    $input.change(function(){
        $(this).parent().toggleClass('checked');
    })
}

scroll('.win-category .first');
scroll('.win-category .second');
scroll('.win-category .third');



$('.nav .navlogin').first().addClass('active');
$('.no-login').css('display','none');
$('.a-login').css('display','block');




